/* Copyright (C) 2013 Dr2co
 *
 * Created with IntelliJ IDEA.
 * User: Dr2co
 * Date: 01.07.13
 *
 *  pt-emu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  pt-emu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with pt-emu.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.configs.shedule;

import com.aionemu.commons.utils.xml.JAXBUtil;
import com.aionengine.gameserver.model.templates.cron.CronSheduleTime;
import javolution.util.FastList;
import org.apache.commons.io.FileUtils;

import javax.xml.bind.annotation.*;
import java.io.File;

/**
 * @author Dr2co
 */


@XmlRootElement(name = "cron_schedule")
@XmlAccessorType(XmlAccessType.FIELD)
public class CronSchedule {

    @XmlElement(required = true)
    private FastList<Schedule> schedule;

    public FastList<Schedule> getSchedule() {
        if (schedule == null) {
            schedule = FastList.newInstance();
        }
        return schedule;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "schedule")
    public static class Schedule {

        @XmlAttribute(required = true)
        private int id;
        @XmlAttribute(required = true)
        private String name;
        @XmlElement(required = true)
        private FastList<CronSheduleTime> time;

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public FastList<CronSheduleTime> getTime() {
            if (time == null) {
                time = FastList.newInstance();
            }
            return time;
        }
    }

    public static CronSchedule load() {
        CronSchedule ss;
        try {
            String xml = FileUtils.readFileToString(new File("./config/shedule/cron_schedule.xml"));
            ss = JAXBUtil.deserialize(xml, CronSchedule.class);
        } catch (Exception e) {
            throw new RuntimeException("[CRON SCHEDULES] Failed to initialize schedules", e);
        }
        return ss;
    }
}
